<?php
if ($DEBUG) {
	if (isLoggedIn()) {
		echo "Test - you are logged in :)";
		//show_userbox();
	} else {
		echo "Test - you are NOT logged in :(";
	};
};
?>

<h2>Browse hairstylists</h2>

<?php
include "searchstylists.inc.php";

// Set local variables
$per_page = 10; 
if (isset($_GET["page"])) {
	$page = $_GET["page"];
} else {
	$page=1;
};
$start_from = ($page-1) * $per_page; 

// decide on search variables
if (isset($_POST['searching']) || isset($_GET['searching'])) {
	$searching="yes";
	if (isset($_POST['find_country'])) {
		$find_country = $_POST['find_country'];
	} else if (isset($_GET['find_country'])) {
		$find_country = $_GET['find_country'];
	}
	if (isset($_POST['find_region'])) {
		$find_region = $_POST['find_region'];
	} else if (isset($_GET['find_region'])) {
		$find_region = $_GET['find_region'];
	}
	if (isset($_POST['find_city'])) {
		$find_city = $_POST['find_city'];
	} else if (isset($_GET['find_city'])) {
		$find_city = $_GET['find_city'];
	}
	if (isset($_POST['find_hairstylist'])) {
		$find_hairstylist = $_POST['find_hairstylist'];
	} else if (isset($_GET['find_hairstylist'])) {
		$find_hairstylist = $_GET['find_hairstylist'];
	}
	if (isset($_POST['find_salon'])) {
		$find_salon = $_POST['find_salon'];
	} else if (isset($_GET['find_salon'])) {
		$find_salon = $_GET['find_salon'];
	}
}

if ($DEBUG && isset($searching)) {
	echo "Searching";
}

// standard select + joins
$sql_select = 
	"SELECT Name, Telephone, Salon, Country, Region, City";
$sql_body =	
	" FROM Stylists AS s" 
	." INNER JOIN Cities AS c ON s.CityID = c.CityID"
	." INNER JOIN Regions AS r ON s.RegionID = r.RegionID"
	." INNER JOIN Countries AS z ON s.CountryID = z.CountryID";
// include conditions from post search
if (isset($searching)) {
	$where_or_and = " WHERE";
	if ($find_country != -1) {
		$sql_body = $sql_body.$where_or_and." s.CountryID = ".$find_country;
		$where_or_and = " AND";
	}
	if ($find_region != -1) {
		$sql_body = $sql_body.$where_or_and." s.RegionID = ".$find_region;
		$where_or_and = " AND";
	}
	if ($find_city != -1) {
		$sql_body = $sql_body.$where_or_and." s.CityID = ".$find_city;
		$where_or_and = " AND";
	}
	if (trim($find_hairstylist) != "") {
		// full table scan... nice!
		$sql_body = $sql_body.$where_or_and." s.Name LIKE '%".$find_hairstylist."%'";
		$where_or_and = " AND";
	}
	if (trim($find_salon) != "") {
		//  full table scan... nice!
		$sql_body = $sql_body.$where_or_and." s.Salon LIKE '%".$find_salon."%'";
		//$where_or_and = " AND";
	}
}
// sorting + limit per page
$sql = $sql_select.$sql_body
	." ORDER BY name ASC LIMIT $start_from, $per_page"; 
if ($DEBUG) {
	echo "<p>".$sql."</p>";
};
$rs_result = mysql_query ($sql,$conn); 
?> 

<table>
    <tr>
        <td>Name</td>
        <td>Phone</td>
        <td>Salon Name</td>
        <td>Country</td>
        <td>Province/State</td>
        <td>City</td>
    </tr>
<?php 
// count
while ($row = mysql_fetch_assoc($rs_result)) { 
// count++;
?> 
	<tr>
		<td><?php echo $row["Name"]; ?></td>
		<td><?php echo $row["Telephone"]; ?></td>
		<td><?php echo $row["Salon"]; ?></td>
        <td><?php echo $row["Country"]; ?></td>
        <td><?php echo $row["Region"]; ?></td>
        <td><?php echo $row["City"]; ?></td>
	</tr>
<?php 
}; 
?> 
</table>

<?php 
//
// Compute previous/next links
//
// TODO: can we not run this twise
$sql = "SELECT COUNT(s.id)".$sql_body;
$rs_result = mysql_query($sql,$conn); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / $per_page); 

if ($page > 1) {
	$previous = $page-1;
	$link = "<a href='".$PHP_SELF."?page=".$previous;
	if (isset($searching))
		$searchParams = getSearchParams($searching, $find_country, $find_region, $find_city, $find_hairstylist, $find_salon);
	else
		$searchParams = "";
	$link = $link.$searchParams;
	$link = $link."'>.previous.</a> ";
	echo $link;
};
if ($total_pages > $page) {
	$next = $page+1;
	$link = "<a href='".$PHP_SELF."?page=".$next;
	if (isset($searching))
		$searchParams = getSearchParams($searching, $find_country, $find_region, $find_city, $find_hairstylist, $find_salon);
	else
		$searchParams = "";
	$link = $link.$searchParams;
	$link = $link."'>.next.</a> ";
	echo $link;
};

//for ($i=1; $i<=$total_pages; $i++) { 
//            echo "<a href='".$PHP_SELF."?page=".$i."'>".$i."</a> "; 
//}; 

echo "<br/><br/><b>Still can't find what you are looking for?  Add it to Rate My Hairstylist!</b><br/>";
echo "To add a hairstylist, <a href='addhairstylist.php'>click here</a>.";
?>

<?php

function getSearchParams($searching, $find_country, $find_region, $find_city, $find_hairstylist, $find_salon) {
	$link_params = "";
	if (isset($searching)) {
		$link_params = $link_params."&searching=".$searching;
		$link_params = $link_params."&find_country=".$find_country;
		$link_params = $link_params."&find_region=".$find_region;
		$link_params = $link_params."&find_city=".$find_city;
		$link_params = $link_params."&find_hairstylist=".$find_hairstylist;
		$link_params = $link_params."&find_salon=".$find_salon;
	}
	return $link_params;
}

?>